
// 每次都是记录最外层的烂橘子,然后在记录感染的橘子,
// 最后判断一下是否还有橘子烂不了, 最后发现确实还有橘子烂不了就是-1
; (function () {
    // return
    /**
   * @param {number[][]} grid
   * @return {number}
   */
    var orangesRotting = function (grid) {
        let queue = {}
        let m = grid.length
        let n = grid[0].length

        let scan = (grid) => {
            let queue2 = new Set()
            let queue1 = new Set()
            let queue0 = new Set()
            for (let index = 0; index < grid.length; index++) {
                const element = grid[index];

                for (let val = 0; val < element.length; val++) {
                    const item = element[val];
                    if (item == 2) {
                        queue2.add([index, val].join())
                    }
                    if (item == 1) {
                        queue1.add([index, val].join())
                    }
                    if (item == 0) {
                        queue0.add([index, val].join())
                    }
                }

            }
            return {
                queue2,
                queue1,
                queue0,
            }
        }
        // 获取所有的0，1，2的集合
        queue = scan(grid)
        let flag = 0
        let { queue2, queue0, queue1 } = queue
        while (flag <= 101) {

            let newQueue2 = []
            // 获取下一轮感染坐标
            function infect(str) {
                let arr = str.split(',')
                let newQueue = []
                let dir = [[1, 0], [-1, 0], [0, 1], [0, -1]]

                for (const coordinate of dir) {
                    let x = Number(arr[0]) + Number(coordinate[0])
                    let y = Number(arr[1]) + Number(coordinate[1])
                    if (x >= 0 && x < m && y >= 0 && y < n) {
                        newQueue.push([x, y].join())
                    }
                }
                return newQueue
            }

            // 循环之前最外围的烂橘子
            queue2.forEach(element => {
                queue0.add(element)
                queue1.delete(element)
                newQueue2 = newQueue2.concat(infect(element))
            });

            newQueue2 = newQueue2.filter((item) => {
                return !(queue0.has(item))
            })

            queue2 = [...new Set(newQueue2)]
            if (queue2.length === 0 && [...queue1].length === 0) {
                return flag
            }

            flag++
        }
        if ([...queue1].length === 0) {
            return 0
        } else {
            console.log("进入了这里");
            return -1
        }


    };
    let list = [
        [2, 0, 1, 1, 1, 1, 1, 1, 1, 1],
        [1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
        [1, 0, 1, 0, 1, 1, 1, 1, 0, 1],
        [1, 0, 1, 0, 1, 0, 0, 1, 0, 1],
        [1, 0, 1, 0, 1, 0, 0, 1, 0, 1],
        [1, 0, 1, 0, 1, 1, 0, 1, 0, 1],
        [1, 0, 1, 0, 0, 0, 0, 1, 0, 1],
        [1, 0, 1, 1, 1, 1, 1, 1, 0, 1],
        [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
    let a = orangesRotting(list)
    console.log(a)
})();


// 此题,是每次只能干感染上下左右的橘子, 和广度搜索很相似.  但又不完全相似
// 可能多个腐烂的橘子. 所以假设在-1分钟的时候有一个超级起点,
// 所以当前0分钟的时候, 感染当前几个橘子
; (function () {
    return

    /**
     * @param {number[][]} grid
     * @return {number}
     */
    var orangesRotting = function (grid) {
        const direct = [[-1, 0], [0, -1], [1, 0], [0, 1]]
        let queue = [], goodNum = 0
        for (let i = 0; i < grid.length; i++) {
            for (let j = 0; j < grid[0].length; j++) {
                const item = grid[i][j]
                if (item === 1) {
                    goodNum++
                } else if (item === 2) {
                    queue.push([i, j])
                }
            }
        }
        if (goodNum === 0) {
            return 0
        }
        let time = -1
        while (queue.length) {
            const temp = []
            queue.forEach(([x1, y1]) => {
                direct.forEach(([x2, y2]) => {
                    const a = x1 + x2, b = y1 + y2
                    if (a >= 0 && a < grid.length && b >= 0 && b < grid[0].length && grid[a][b] === 1) {
                        grid[a][b] = 2
                        goodNum--
                        temp.push([a, b])
                    }
                })
            })
            time++
            queue = temp
        }
        return goodNum === 0 ? time : -1
    };
})();